<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易四则运算器</title>
    <!-- 引入当前libs目录里的Vue库文件 -->
    <script src="../libs/vue.global.min.js"></script>
</head>

<body>
    <div id="app">
        <h1>简易四则运算器</h1>
        <label for="num1">第一个数字:</label>
        <input type="number" id="num1" v-model="num1">
        <label for="operator">运算符:</label>
        <select id="operator" v-model="operator">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <label for="num2">第二个数字:</label>
        <input type="number" id="num2" v-model="num2">
        <button @click="calculate">计算</button>
        <p v-if="result!== null">结果: {{ result }}</p>
    </div>

    <script>
        const { createApp } = Vue;
        const app = createApp({
            data() {
                return {
                    num1: 0,
                    num2: 0,
                    operator: '+',
                    result: null
                };
            },
            methods: {
                calculate() {
                    const num1 = parseFloat(this.num1);
                    const num2 = parseFloat(this.num2);
                    switch (this.operator) {
                        case '+':
                            this.result = num1 + num2;
                            break;
                        case '-':
                            this.result = num1 - num2;
                            break;
                        case '*':
                            this.result = num1 * num2;
                            break;
                        case '/':
                            if (num2 === 0) {
                                alert('除数不能为零');
                                this.result = null;
                            } else {
                                this.result = num1 / num2;
                            }
                            break;
                    }
                }
            }
        });
        app.mount('#app');
    </script>
</body>

</html>